﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Common.Extensions
{
    public static class EnumExtensions
    {
        public static string GetEnumDescription(this Enum enumValue)
        {
            if (enumValue == null)
                return string.Empty;
            string value = enumValue?.ToString();
            FieldInfo field = enumValue?.GetType()?.GetField(value);
            object[] obj = field.GetCustomAttributes(typeof(DescriptionAttribute), false);  //获取描述属性
            if (obj == null || obj.Length == 0)  //当未设置描述属性时，直接返回名称
                return value;
            var descriptionAttribute = (DescriptionAttribute)obj[0];
            return descriptionAttribute.Description;
        }
    }
}
